// 统一所有模块中的删除功能
import { Notify, Dialog } from 'quasar';
import type { ResponseData } from '@/api/type.ts';

interface RequestFn {
  (params: any): Promise<ResponseData>;
}

export function useRemoveItem<
  T extends RequestFn,
  U extends (...args: any) => void
>(
  request: T,
  params: Parameters<T>[0],
  msg: string,
  callback: U,
  ...callbackParams: Parameters<U>
) {
  Dialog.create({
    title: '提示',
    message: msg,
    ok: { label: '确认', color: 'negative' },
    cancel: { label: '取消', flat: true },
    persistent: true
  }).onOk(async () => {
    try {
      const res = await request(params);
      if (res.code === 200) {
        Notify.create({
          type: 'positive',
          message: '删除成功',
          position: 'top'
        });
        callback(...callbackParams);
      } else {
        throw new Error(res.data);
      }
    } catch (error: any) {
      Notify.create({
        type: 'negative',
        message: error.message,
        position: 'top'
      });
    }
  });
}
